package com.adopapa.nezhacloud.gateway.utils;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;

import com.adopapa.nezhacloud.common.JsonUtil;
import com.adopapa.nezhacloud.common.RestResponse;

import reactor.core.publisher.Mono;


public final class ResponseUtils {

	public static Mono<Void> response(ServerHttpResponse response, String contentType, HttpStatus status, //
			Object value, int code) {
		try {
			response.setStatusCode(status);
			response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
			RestResponse result = RestResponse.other(code, value.toString());
			DataBuffer dataBuffer = response.bufferFactory().wrap(JsonUtil.bean2Json(result).getBytes());
			return response.writeWith(Mono.just(dataBuffer));
		} catch (Exception e) {
			return Mono.empty();
		}
	}

	public static Mono<Void> response(ServerHttpResponse response, HttpStatus status, Object value, int code) {
		return response(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
	}

	public static Mono<Void> response(ServerHttpResponse response, Object value, int code) {
		return response(response, HttpStatus.OK, value, code);
	}

	public static Mono<Void> response(ServerHttpResponse response, Object value) {
		return response(response, HttpStatus.OK, value, RestResponse.OTHER);
	}

}
